﻿
/*******************************************************************************************
// ©2009 Microsoft Corporation.  This code is provided under the Microsoft Public License.
*******************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Rtc.Collaboration;
using System.Configuration;
using Microsoft.Rtc.Collaboration.AudioVideo;
using Microsoft.Rtc.Signaling;

namespace ClientBilling
{
    public class BilledCallPlacer
    {
        #region Instance Variables
        ApplicationEndpoint _appEndpoint;
        Conversation _conversation;
        InstantMessagingCall _imCall;
        AudioVideoCall _avCall;
        #endregion

        public BilledCallPlacer(ApplicationEndpoint appEndpoint)
        {
            _appEndpoint = appEndpoint;
        }

        /// <summary>
        /// Initiate a billed call to a client, starting by billing
        /// to a specified case.
        /// </summary>
        public void PlaceCallForCase(Case initialCase)
        {
            // We create a call between the lawyer and the client by
            // initiating a conference and inviting the lawyer and the client to it.
            // Once we've got an instant message call going, we start
            // billing, show the extended window to the lawyer,
            // and establish an audio call.

            if (initialCase == null)
            {
                throw new ArgumentNullException("initialCase cannot be null.");
            }

            if (string.IsNullOrEmpty(initialCase.Client.SipUri))
            {
                throw new ArgumentException("Client must have a SIP URI.");
            }

            // Determine the client for the chosen case and set the client's
            // primary case to be the chosen case.
            Client calledClient = initialCase.Client;
            calledClient.PrimaryCase = initialCase;

            // Place a new outgoing call to the client.
            Conversation conv = new Conversation(_appEndpoint);
            AudioVideoCall call = new AudioVideoCall(conv);
            call.EndEstablish(call.BeginEstablish(calledClient.SipUri, null, null, null));

            // Perform a self-transfer (i.e., transfer the call back to our own SIP URI) so that it will
            // be picked up by the IncomingCallRouter and routed to the lawyer.
            call.EndTransfer(call.BeginTransfer(call, null, null)); 
        }
    }
}
